package zhixin.Utils;

import net.sourceforge.tess4j.ITesseract;
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;

/**
 * @program: demo01
 * @description:
 * @author: 致心
 * @create: 2020-08-28 10:50
 **/
public class UtilsIndex {

    public static void wtriteCookie(WebDriver dr){
        Cookies.wtriteC(dr);
    }

    public static void getCookie(WebDriver dr, String url) throws Exception {
        Cookies.getC(dr, url);
    }

    /**
     * @Description 识别图片文字
     * @param img
     * @Author: 致心
     * @Date: 2020/8/28 12:45
     */
    public static String imgToWord(BufferedImage img) throws TesseractException, IOException {
        ITesseract ins = new Tesseract();
        // 根据需求选择语言库 chi_sim ：简体中文， eng
        ins.setLanguage("eng");
        // 设置训练库的位置，这个用maven的时候jar里面就有，复制出来就好了
        ins.setDatapath("tessdata");
        ImageIO.write(img,"png",new File("image.png"));
        img=ImgToWord.grayImage(img);
        ImageIO.write(img,"png",new File("image1.png"));
        return ins.doOCR(img);
    }

    /**
     * @Description 元素转为图片
     * @param driver
     * @param webElement
     * @Author: 致心
     * @Date: 2020/8/28 12:44
     */
    public static BufferedImage elementToImg(WebDriver driver, WebElement webElement) throws IOException {
        UtilsIndex.webMoveToElement(driver,webElement);
        return EleToImg.createElementImage(driver, webElement);
    }

    /**
     * @param driver
     * @param element
     * @Description 页面移动到指定元素
     * @Author: 致心
     * @Date: 2020/8/28 11:19
     */
    public static void webMoveToElement(WebDriver driver, WebElement element) {
        Actions action = new Actions(driver);
        action.moveToElement(element);
    }

    /**
     * 本地文件（图片、excel等）转换成Base64字符串
     *
     * @param imgPath
     */
    public static String convertFileToBase64(String imgPath) {
        byte[] data = null;
        // 读取图片字节数组
        try {
            InputStream in = new FileInputStream(imgPath);
            System.out.println("文件大小（字节）=" + in.available());
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 对字节数组进行Base64编码，得到Base64编码的字符串
        BASE64Encoder encoder = new BASE64Encoder();
        String base64Str = encoder.encode(data);
        return base64Str;
    }

    /**
     * 将base64字符串，生成文件
     */
    public static File convertBase64ToFile(String fileBase64String, String filePath, String fileName) {

        BufferedOutputStream bos = null;
        FileOutputStream fos = null;
        File file = null;
        try {
            File dir = new File(filePath);
            if (!dir.exists() && dir.isDirectory()) {//判断文件目录是否存在
                dir.mkdirs();
            }

            BASE64Decoder decoder = new BASE64Decoder();
            byte[] bfile = decoder.decodeBuffer(fileBase64String);

            file = new File(filePath + File.separator + fileName);
            fos = new FileOutputStream(file);
            bos = new BufferedOutputStream(fos);
            bos.write(bfile);
            return file;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }
}
